/**
 * Created by ZT on 2018/5/3 0003.
 */
(function ($) {
    $.fn.paginator = function (options) {
        //this指向当前的选择器
        var config = {
            url: "",
            pageParent: "",
            vues:'',
            totalBars: -1,
            total:'',
            limit: -1,
            boxContent:'',
            offset: 1,
            callback: null
        };
        //合并参数
        var opts = $.extend(config, options);

        opts.totalBars = Math.ceil(opts.total / opts.limit);
        //计算按钮的总个数

        //获取offset参数
        var queryString = function () {
            var offset = opts.offset;
            return parseInt(offset);
        }

        //ajax核心方法，用于分页的数据操作
        var ajaxCore = function (offset, fn) {
            opts.vues.fetch({
                url : opts.url,
                methods : 'POST',
                params:{
                    page : offset,
                    per_page : opts.limit
                },
            }).then(function (response) {
                opts.total = response.data.total;
                opts.vues.dataTable = response.data.rows;
                opts.totalBars = Math.ceil(opts.total / opts.limit);
            });
        };

        //重新装配分页按钮
        var pageCore = function (offset) {
            if (opts.offset == offset) {
                return;
            } //如果是当前页面，那么就什么事都不用干了！
            else {
                ajaxCore(offset, opts.callback);
                $(opts.pageParent).empty();
                //否则，清空所有的节点，重新向DOM插入新的分页按钮
                var output = "";
                var nextBar = offset == opts.totalBars ? '<li class=\"am-disabled\"><a href="javascript:void(0);">»</a></li>' : '<li><a href="javascript:void(0);" attr='+(offset + 1) +'>»</a></li>';
                var preBar = offset == 1 ? '<li class=\"am-disabled\"><a href="javascript:void(0);">«</a></li>' : '<li><a href="javascript:void(0);"  attr='+ (offset - 1)+'>«</a></li>';
                //组装向上一个节点和下一页节点
                if (opts.totalBars > 7) {
                    if (offset < 5) {
                        output += preBar;
                        for (var i = 1; i <= 5; i++) {
                            if (i == offset) {
                                output +='<li class="am-active"><a  href="javascript:void(0);" attr='+ offset +'>' + offset +'</a></li>';
                            } else {
                                output += '<li><a href="javascript:void(0);" attr='+ i + '>' + i + '</a></li>';
                            }
                        }
                        output += "<li><span>...</span></li>";
                        output += '<li><a  href="javascript:void(0);"  attr =' + (opts.totalBars) + '>' + (opts.totalBars) +'</a></li>' + nextBar;
                    } else if (offset >= 5 && offset <= opts.totalBars - 4) {
                        //当页面大于7个的时候，那么在第五个和倒数第五个时，执行
                        output += preBar;
                        output += '<li><a href="javascript:void(0);"  attr ="1">' + 1 +'</a></li>';
                        //第一个
                        output += "<li><span>...</span></li>"; //省略号

                        output += '<li><a href="javascript:void(0);"  attr =' + (offset - 1) + '>' + (offset - 1) +'</a></li>';

                        output += '<li class=\"am-active\"><a  href="javascript:void(0);"  attr =' + (offset) + '>' + offset + '</a></li>';

                        output += '<li><a href="javascript:void(0);"  attr =' + (offset+1) + '>' + (offset + 1) + '</a></li>';

                        output += "<li><span>...</span></li>"; //省略号;

                        output += '<li><a href="javascript:void(0);"  attr =' + (opts.totalBars) + '>' + (opts.totalBars) +'</a></li>'; //尾页

                        output += nextBar;

                    } else if (offset > opts.totalBars - 4 && offset <= opts.totalBars) {
                        //当页面位于倒数第四个时候
                        output += preBar;
                        output += '<li><a href="javascript:void(0);"  attr ="1">' + 1 + '</a></li>' + '<li><span>...</span></li>';

                        for (var j = 4; j >= 0; j--) {
                            if (opts.totalBars - j == offset) {
                                output += '<li class=\"am-active\"><a href="javascript:void(0);"  attr =' + (opts.totalBars - j) + '>' + (opts.totalBars - j) + '</a></li>';
                            } else {
                                output += '<li><a href="javascript:void(0);" attr =' + (opts.totalBars - j) + '>' + (opts.totalBars - j) + '</a></li>';
                            }
                        }
                        output += nextBar;
                    } else {
                        console.log("分页数据出错！");
                        return;
                    }
                } else {
                    output += preBar;
                    for (var i = 1; i <= opts.totalBars; i++) {
                        if (i == offset) {
                            output += '<li class=\"am-active\"><a href="javascript:void(0);"  attr =' + (i + 1) + '>' + offset+ '</a></li>';
                        } else {
                            output += '<li><a href="javascript:void(0);"  attr='+i+'>' + i+ '</a></li>';
                        }
                    }
                    output += nextBar;
                }
                $(opts.pageParent).append(output);
                opts.offset = offset; //将偏移量赋值给config里面的offset
            }
        }

        //清理函数，防止多绑定事件和重新计算分页
        var clear = function () {
            $(opts.pageParent).empty().undelegate();
        }


        //初始化装配分页按钮
        var init = function (fn) {
            if (typeof (fn) != "function") {
                console.log("将不能正确的执行回调函数");
            } else {
                opts.callback = fn;
            }
            clear();
                var preBar = '<li class=\"am-disabled\"><a href="javascript:void(0);">«</a></li>';
                //上一页,（禁用的效果）
                //如果只有一页，那么禁用下一页
                var nextBar = opts.totalBars > 1 ? '<li><a href="javascript:void(0)" attr=' + 2 + '>»</a></li>' : '<li class="am-disabled"><a href="javascript:void(0)">»</a></li>';
                //最后一页
                var output = '<li class=\"am-active\"><a href="javascript:void(0)" attr="1">1</a></li>';

                if (opts.totalBars <= 7) {
                    for (var i = 1; i < opts.totalBars; i++) {
                        output += '<li><a href="javascript:void(0)" attr =' + (i + 1) + '>' + (i + 1) + '</a></li>';
                    }
                } else {
                    for (var j = 1; j < 5; j++) {
                        output += '<li><a href="javascript:void(0)" attr='+(j + 1) +'>' + (j + 1) + '</a></li>';
                    }
                    output += "<li><span>...</span></li>";
                    output += '<li><a href="javascript:void(0)">' + (opts.totalBars) +'</a></li>';
                }
                $(opts.pageParent).delegate("a","click", function () {
                    var offset =parseInt($(this).attr("attr"));
                    pageCore(offset);
                });
                $(opts.pageParent).append(preBar + output + nextBar);
        };
        init(opts.callback);//初始化分页引擎
    }
}(window.jQuery));

/*公共的分页调用接口
*  response 响应
*   url  请求地址
*  data  //数据内内容
*  row  ｛page,per_page,total｝
*  table  定义的翻页content
*
* */
function initPagination(response,url,table,vue) {
     vue.dataTable = response.data.rows;
     vue.row.total = response.data.total;
    $(table).paginator({
        url: url,
        pageParent: table,
        total: response.data.total,
        vues : vue,
        boxContent:response.data.rows,
        limit  : vue.row.per_page,
        offset : vue.row.page,
        callback: function (data) {
        }
    });
}